/*
 * (c) 2007, Delsi Interactive
 * User: Usuario Deltatek
 * Date: 22/04/2007
 * Time: 09:53 p.m.
 */
 
/*
 * Clip - the heart of this editor
 * Every clip can have a lot of childs,
 * except the animation clips.
 * In that case the childs would be
 * frames.
 */

using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;

using System.Collections.Generic;

namespace CSE
{
    /*
     * ClipChild
     */
    public class ClipChild
    {
        public int x=0;
        public int y=0;
        public string name="";
        public int start=0;
        public int end=0;
        
        /*
         * Constructor
         */
        public ClipChild(int _x, int _y, string _name, int _start, int _end)
        {
            x  = _x;
            y  = _y;
            name = _name;
            start = _start;
            end = _end;
        }
    }
    
	/// <summary>
	/// Description of Clip.
	/// </summary>
	public class Clip
	{
		/*
		 * Constants
		 */
		public static readonly int SIZE = 32;
		
		/*
		 * Variables
		 */
		public int type = Workspace.TYPE_CLIP;
		public bool usebitmap = false;
		public string name = "";
		public Rectangle r;
		public List<ClipChild> childs = null;
		
		/*
		 * Constructor
		 */
		public Clip (string _name, bool _usebitmap)
		{
			name = _name;
			usebitmap = _usebitmap;
			childs = new List<ClipChild>();
			r = new Rectangle(0,0,SIZE,SIZE);
		}
		
		/*
		 * AdjustSize
		 */
		public void AdjustSize ()
		{
		    /*
		    int i = 0;
		    int xmin = 99999999;
		    int ymin = 99999999;
		    int xmax = -99999999;
		    int ymax = -99999999;
		    Clip _clip = null;
		    
		    for (i=0; i<childs.Count; i++)
		    {
		        _clip = Workspace.GetClipFromName(childs[i].name);
		        
		        if (usebitmap)
    		    {
    		    }
    		    else if (type == Workspace.TYPE_CLIP)
    		    {
    		        if (r.X + childs[i].x < xmin)
	                {
                        xmin = r.X + childs[i].x;
                    }
    		        if (r.X + childs[i].x + _clip.r.Width > xmax)
    		        {
    		            xmax = r.X + childs[i].x + _clip.r.Width;
    		        }
    		        
    		        if (r.Y + childs[i].y < ymin)
	                {
                        ymin = r.Y + childs[i].y;
                    }
    		        if (r.Y + childs[i].y + _clip.r.Height > ymax)
    		        {
    		            ymax = r.Y + childs[i].y + _clip.r.Height;
    		        }
    		    }
		    }
		    
		    if (childs.Count > 0)
		    {
		        if (type == Workspace.TYPE_CLIP)
		        {
    		        r.X = r.X - xmin;
		            r.Width = xmax - r.X;
		            r.Y = r.Y - ymin;
		            r.Height = ymax - r.Y;
		        }
		    }
		    */
		}
		
		/*
		 * Copy
		 */
		public void Copy (Clip other)
		{
		    int i = 0;
		    
			r.X = other.r.X;
			r.Y = other.r.Y;
			r.Width = other.r.Width;
			r.Height = other.r.Height;
			
			type = other.type;
			
			for (i=0; i<other.childs.Count; i++)
		    {
				if (type == Workspace.TYPE_ANIMCLIP)
				{
				    childs.Add(new ClipChild(other.childs[i].x,other.childs[i].y,
				                            other.childs[i].name,
				                            childs.Count, childs.Count));
                }
				else
				{
				    childs.Add(new ClipChild(other.childs[i].x,other.childs[i].y,
				                            other.childs[i].name,
				                            0, 0));
				}
			}
		}
		
		/*
		 * SwapChild
		 */
		public void SwapChild(int indexA, int indexB)
        {
            ClipChild temp = childs[indexA];
            childs[indexA] = childs[indexB];
            childs[indexB] = temp;
        }
		
		/*
		 * DrawBitmapClip
		 */
		public void DrawBitmapClip (Graphics g, Bitmap bmp, ImageAttributes attr, int x, int y, int w, int h)
		{
		    /*
		     * Simple bitmap clip
		     */
		    if (usebitmap)
		    {
		        Rectangle dstRect = new Rectangle(x, y, w, h);
	    	    g.DrawImage(bmp,
                            dstRect,
    		            	r.X, r.Y,
                            r.Width,
    		            	r.Height,
							GraphicsUnit.Pixel,
							attr);
		    }
		}
		
		/*
		 * DrawStaticClip
		 */
		public void DrawStaticClip (Graphics g, Bitmap bmp, ImageAttributes attr, int ox, int oy, int zoom)
		{
		    int i=0;
            ClipChild cc = null;
            Clip cc_clip = null;
            
            for (i=0; i<childs.Count; i++)
            {
                cc = childs[i];
                cc_clip = Workspace.GetClipFromName(cc.name);
                
                if (cc_clip.usebitmap)
                {
                    cc_clip.DrawBitmapClip(g, bmp, attr, ox + (cc.x*zoom), oy + (cc.y*zoom), cc_clip.r.Width*zoom, cc_clip.r.Height*zoom);
                }
                else if (cc_clip.type == Workspace.TYPE_CLIP)
                {
                    cc_clip.DrawStaticClip(g, bmp, attr, ox + (cc.x*zoom), oy + (cc.y*zoom), zoom);
                }
            }
		}
	}
}
